<?php
class Default_Model_DbTable_AclResourcesAccessView extends Zend_Db_Table_Abstract
{
	protected $_name = 'acl_resources_access_view';

	protected $_primary =  array('id');


	public function getById($id)
	{
		$rowSet = $this->find($id);
		return $rowSet->current();
	}

	public function getAccessTree($id)
	{

		$data = $this->find($id)->current()->toArray();
		$arr = $this->_recursiveAccess($data['resource_id']);
		$res_arr = array();
		// 刪除 role_id == null
		foreach($arr as $row){
			if ($row['role_id'])
				$res_arr[]=$row;
		}
		return $res_arr;
	}

	private function _recursiveAccess($id)
	{
		$res = $this->fetchAll("resource_id='$id' ")->toArray();
		if ($res)
			$res =  array_merge($this->_recursiveAccess($res[0]['parent_id']),$res);

		return $res;
	}
}
